import axios from 'axios'
import { message } from 'antd'
import { resType } from '@/types'

const service = axios.create({
  baseURL: '',
  timeout: 5000,
})

service.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers['Authorization'] = 'Bearer ' + token
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    const { errno, data, msg } = response.data as Partial<resType>
    if (errno !== 0) {
      message.error(msg)
      return Promise.reject(msg)
    }
    return data as any
  },
  error => {
    return Promise.reject(error)
  }
)

export default service
